import config from '../config.js'
export const request = (options) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: "数据加载中",
			mask: true
		})
		uni.request({
			url: baseUrl.HTTP_REQUEST_URL + options.url, //接口地址：前缀+方法中传入的地址
			method: options.method || 'GET', //请求方法：传入的方法或者默认是“GET”
			data: options.data || options.params || {}, //传递参数：传入的参数或者默认传递空集合
			//	dataType: options.dataType || "json",
			header: {
				"Content-Type": "application/json",
				'Token': uni.getStorageSync('token') || '', //自定义请求头信息
				'Authorization': "Bearer " + uni.getStorageSync('authToken') || ''
				// 'content-type':options.headers['Content-Type'] || 'application/x-www-form-urlencoded;charset=UTF-8'
			},
			success: (res) => {
				setTimeout(() => {
					uni.hideLoading()
				}, 200)
				let {
					code,
					data,
					msg,
					returnCode
				} = res.data
				if (code == 0 || returnCode == 2000) {
					resolve(data);
				} else {
					resolve(res.data);
					uni.showToast({
						title: msg,
						icon: 'none',
						mask: true,
						duration: 2000,
						complete: (res) => {
							if (code == -3 || code == 9001) {
								login();
							}
						}
					});
				}


			},
			// 这里的接口请求，如果出现问题就输出接口请求失败
			fail: (err) => {
				setTimeout(() => {
					uni.hideLoading()
				}, 200)
				console.log(baseUrl.api + options.url)
				console.log(err)
				reject(err)
			}
		})
	})
}


export default request